package com.rd.student.jike.class2.lihongming.Slolution3;

/**
 * @Auther:Heinrich
 * @Date:2023/7/28-07-28-9:29
 * @Description:com.rd.student.jike.class2.lihongming.exercise3
 * @version:1.0
 */
public class Question7 {
    public static void main(String[] args) throws InterruptedException {
        int[] ai = {1,2,3,4,5,6,7};
        String[] ac = {"A","B","C","D","E","F","G"};
        Temp temp = new Temp(2);
        PrintArrayInt printArray2 =new PrintArrayInt(ai,temp);
        Thread.sleep(1);
        PrintArrayStr printArray1 = new PrintArrayStr(ac,temp);
        printArray1.start();
        printArray2.start();
    }


}
class PrintArrayStr extends Thread{
    private String[] arr ;
    Temp temp ;

    public PrintArrayStr(String[] arr, Temp temp) {
        this.arr = arr;
    }

    @Override
    public void run() {
        for (int i = 0; i < arr.length; i++) {
            synchronized (this){
                while (temp.getTemp() != 1){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(arr[i]);
                temp.setTemp(2);
                this.notifyAll();
            }
        }
    }
}
class PrintArrayInt extends Thread{
    private int[] arr ;
    Temp temp;

    public PrintArrayInt(int[] arr,Temp temp) {
        this.arr = arr;
        this.temp =temp;
    }

    @Override
    public void run() {
        for (int i = 0; i < arr.length; i++) {
            synchronized (this){
                while (temp.getTemp() != 2){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(arr[i]);
                temp.setTemp(1);
                this.notifyAll();
            }
        }
    }

}
class Temp{
    int temp;

    public Temp(int temp) {
        this.temp = temp;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }
}